package com.zjhuang.mybatis_demo.controller;

import com.zjhuang.mybatis_demo.entity.User;
import com.zjhuang.mybatis_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huang
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> list() {
         return userService.getAll();
    }

    @GetMapping("{id}")
    public User getUser(@PathVariable int id) {
        return userService.getById(id);
    }

    @GetMapping("name/{name}")
    public User getUserByName(@PathVariable String name) {
        return userService.getByName(name);
    }

    @PostMapping
    public User checkLogin(@RequestBody User user) {
        return userService.checkLogin(user);
    }

    @PostMapping("add")
    public int addUser(@RequestBody User user) {
        int i = userService.addUser(user);
        System.out.println(user);
        return i;
    }
}
